package com.hmall.Listener;

import com.hmall.constants.MQConstants;
import com.hmall.service.ISearchService;
import com.hmall.service.impl.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ItemListener {

    @Autowired
    private ISearchService iSearchService;

    //监听新增或修改队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.ITEM_INSERT_QUEUE),
            exchange = @Exchange(name = MQConstants.ITEM_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = MQConstants.ITEM_INSERT_KEY
    ))
    public void listenerInsertOrUpdate(Long id){
        iSearchService.findById(id);
        log.info(id+"修改成功");
    }

    //监听删除队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.ITEM_DELETE_QUEUE),
            exchange = @Exchange(name = MQConstants.ITEM_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = MQConstants.ITEM_DELETE_KEY
    ))
    public void listenerDelete(long id){
        iSearchService.deleteById(id);
        log.info(id+"删除成功");
    }

}
